package com.homegoods.home.fragments;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.ui.view.IView;
import com.homegoods.home.R;
import com.homegoods.home.databinding.HomeFragmentHomeBinding;
import com.homegoods.home.location.AMapLocationImpl;
import com.homegoods.home.presenters.HomePresenter;
import com.homegoods.resource.Constant;
import com.jakewharton.rxbinding2.view.RxView;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.location.RequestLocationHelper;
import com.kedll.library.location.RequestLocationHelper.OnLocationCompletedListener;
import com.kedll.library.location.entity.LocationEntity;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.RxBus;

import java.util.List;
import java.util.concurrent.TimeUnit;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;


/**
 * @author QiQi
 */
//@RuntimePermissions
@Route(path = ARouterPath.PATH_FRAGMENT_HOME)
public class HomeFragment extends KBaseFragment<HomeFragmentHomeBinding, HomePresenter> implements IView, OnLocationCompletedListener {
    RequestLocationHelper locationHelper;
//    private String city;
    private String lnglat;


    @Override
    protected HomePresenter getPresenter() {
        return new HomePresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.home_fragment_home;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent(Bundle savedInstanceState) {
        /*del shibo.zheng 19/06/25 start*/
//        locationHelper = new RequestLocationHelper(getActivity(), this);
//        locationHelper.setLocationInterface(new AMapLocationImpl(getContext(), locationHelper));

//        locationHelper.setCityTextView(mBinding.tvCity);

        //选择城市
/*        RxView.clicks(mBinding.tvCity)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .compose(bindToLifecycle())
                .subscribe(o -> ARouter.getInstance()
                        .build(ARouterPath.PATH_HOT_CITY)
                        .withTransition(R.anim.slide_left_in, R.anim.hold)
                        .navigation(getActivity()));*/
        /*del shibo.zheng 19/06/25 end*/

        //搜索
        RxView.clicks(mBinding.tvSearch)
                .compose(bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> ARouter.getInstance()
                        .build(ARouterPath.PATH_SEARCH)
                        .navigation());

        /*del shibo.zheng 19/06/25 start*/
        //城市选择返回
/*        RxBus.getDefault().toFlowable(Bundle.class)
                .compose(bindToLifecycle())
                .subscribe(bundle -> {
//                    page = 1;
                    city = bundle.getString("result");
                    mBinding.tvCity.setText(city);
                });*/

        //发起定位
//        HomeFragmentPermissionsDispatcher.startLocationWithPermissionCheck(this);
        /*del shibo.zheng 19/06/25 end*/
        //获取app全局配置信息
        mPresenter.startAppGetDeviceConfig();

        mPresenter.getMenuItems();

    }

    @Override
    public void showLoad(String msg) {

    }

    @Override
    public void hideLoad() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK && data == null) {
            return;
        }

        if (requestCode == 100) {
            String result = data.getStringExtra(Constant.SCAN_RESULT_KEY);
            ARouter.getInstance()
                    .build(ARouterPath.PATH_SEARCH_RESULT)
                    .withString("searchContent", result)
                    .navigation();
        }
    }

    /*del shibo.zheng 19/06/25 start*/
/*    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        HomeFragmentPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }*/
    /*del shibo.zheng 19/06/25 end*/


    @Override
    public void onCompleted(LocationEntity entity) {
//        city = entity.getCity();      /*del shibo.zheng 19/06/25*/
        lnglat = entity.getLngLat();
        locationHelper.stopLocation();
    }

    @Override
    public void setHistoryLocation(LocationEntity entity) {
//        city = entity.getCity();      /*del shibo.zheng 19/06/25*/
        lnglat = entity.getLngLat();
//        mBinding.tvCity.setText(city);    /*del shibo.zheng 19/06/25*/
    }

    @Override
    protected void lazyLoad() {
    }

    /*del shibo.zheng 19/06/25 start*/
/*    @NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void startLocation() {
        locationHelper.startLocation(0);
    }*/
    /*del shibo.zheng 19/06/25 end*/

    @Override
    public void initViewPager(List<Fragment> fragments, String[] titles) {  // titles ==>> 箱包服饰、宅品美食、家居家纺......
        MyUtils.bindViewPagerAndTabLayout(getFragmentManager(), fragments, titles, findViewById(R.id.home_viewpager), findViewById(R.id.home_tab));
    }
}
